package com.jinke.api.modules.runnable;

import com.jinke.api.modules.api.conf.ApiUrlsConfig;
import com.jinke.api.modules.app.service.IUserOrderTaskService;
import com.jinke.api.modules.app.service.OrderParcelService;
import com.jinke.api.modules.app.service.OrderService;
import com.jinke.api.modules.app.service.UserRechargeService;
import com.jinke.api.modules.app.util.OssUtil;
import com.jinke.api.modules.base.service.ThirdService;
import com.jinke.api.modules.third.ezeeship.service.EzeeShipService;
import com.jinke.api.modules.ums.model.UmsAdmin;
import com.jinke.api.modules.ums.service.ChannelService;
import com.jinke.api.modules.ums.service.IApiLogService;
import com.jinke.api.modules.ums.service.UmsAdminService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.concurrent.locks.ReentrantLock;

public class Account {
    private final ReentrantLock lock = new ReentrantLock();

    @Resource
    private ThirdService thirdService;
    @Resource
    private UmsAdminService umsAdminService;
    @Resource
    private ChannelService channelService;
    @Resource
    private OssUtil ossUtil;
    @Resource
    private ApiUrlsConfig apiUrlsConfig;
    @Resource
    private OrderService orderService;



    @Resource
    private UserRechargeService userRechargeService;
    @Resource
    private EzeeShipService ezeeShipService;
    @Resource
    private IApiLogService iApiLogService;
    @Resource
    private IUserOrderTaskService iUserOrderTaskService;
    /**
     * 扣totalAmount的相关参数都在传入
     *
     */

    private BigDecimal totalAmount;
    private String userId;
    public Account(UmsAdmin  umsAdmin) {
        //判断用户是否有足够的金额下单
        BigDecimal totalAmount = umsAdmin.getBalance().add(umsAdmin.getCreditBalance());
        this.totalAmount = totalAmount;
    }

    //退钱
    public void withdraw(BigDecimal rateAmount,UmsAdmin umsAdmin) {
        lock.lock();
        try {
            //判断用户是否有足够的金额下单
            BigDecimal totalAmount = umsAdmin.getBalance().add(umsAdmin.getCreditBalance());
            if (totalAmount.compareTo(rateAmount) < 0) {
                System.out.println("Insufficient balance");
               // return CommonResult.failed("Insufficient Balance");
            }
              else {
//                BigDecimal rate = BigDecimal.valueOf(Double.parseDouble(selectedRate.getRate()));
//                //计算要扣的钱
//                BigDecimal afterRate = thirdService.calcRate(userId, request.getUserChannelVO(), rate);
//                // 扣钱
//                BigDecimal finalRate = afterRate;
//                if (finalRate != null && BigDecimal.ZERO.compareTo(finalRate) < 0) {
//                    userRechargeService.decrTransfer(userId, finalRate, BalanceLogType.CONSUME, "寄件", order.getOrderNo());
//                }
            }
        } finally {
            lock.unlock();
        }
    }
}
